package com.app.common.adapter

class ItemSelector<T:Any> (private val items:List<T>) {

    private val mapSelected = mutableMapOf<Int,Boolean>()

    fun setSelectValue(position: Int,selected:Boolean){
        mapSelected[position] = selected
    }

    fun select(position: Int){
        setSelectValue(position,true)
    }

    fun unSelect(position: Int){
        setSelectValue(position,false)
    }

    fun selectAll(){
        for (i in items.indices){
            setSelectValue(i,true)
        }
    }

    fun reset(){
        mapSelected.clear()
    }

    fun unSelectAll(){
        for (i in items.indices){
            setSelectValue(i,false)
        }
    }

    val selectedItems:List<T>
        get() = items.filterIndexed { index, v ->
            isSelected(index)
        }

    fun selectedItemCount():Int{
        println("=========selectedItemCount:${mapSelected.size}  ${items.size}")
        return mapSelected.count {
            it.value
        }
    }
    fun isSelected(position: Int) = mapSelected[position]?:false
}